//
//  ThemeManager.swift
//  SCA
//
//  Created by tjun on 2025/9/24.
//


import UIKit

enum AppColor: String {
    
    case background = "mainBackground"
    
    case loginTypeTitleNormal = "loginTypeTitleNormal"
    case loginTypeTitleSelect = "loginTypeTitleSelect"
    
    case labeledTextFieldTitle = "labeledTextFieldTitle"
    case labeledTextFieldInput = "labeledTextFieldInput"
    case labeledTextFieldTip = "labeledTextFieldTip"
    case hex00CCFF = "00CCFF"
    case loginAgreementTitle = "loginAgreementTitle"
    case selectCellBackgroundColor = "selectCellBackgroundColor"
    
    case selectedSegmentTintColor = "selectedSegmentTintColor"
    case segmentedControlTitle = "segmentedControlTitle"
    case segmentedControlSelectTitle = "segmentedControlSelectTitle"
    case segmentedControlBg = "segmentedControlBg"
    
    case tabbar_bg = "tabbar_bg"
    case tabbar_selectTitle = "tabbar_selectTitle"
    case tabbar_title = "tabbar_title"
    case tabbar_separator = "tabbar_separator"

    case newsTitle = "newsTitle"
    case guideTitle = "guideTitle"
    
    case appBlack = "appBlack"
    case appWhite = "appWhite"
    case hex2E3036 = "2E3036"
    case hex2078D5 = "2078D5"
    case hex34C759 = "34C759"
    

    
    
    var value: UIColor {
        guard let color = UIColor(named: self.rawValue) else {
            fatalError("Color \(self.rawValue) not found in Assets")
        }
        return color
    }
}

enum AppImage: String {
    case styleMode = "styleMode"
    case loginLogo = "loginLogo"
    case nav_back = "nav_back"
    case languageIcon = "languageIcon"
    case tokenPriceRefresh = "tokenPriceRefresh"
    
    
    case nav_mine_info = "nav_mine_info"
    case nav_logo = "nav_logo"
    case nav_logout = "nav_logout"
    
    var value: UIImage {
        guard let img = UIImage(named: self.rawValue) else {
            fatalError("Image \(self.rawValue) not found in Assets")
        }
        return img
    }
}

struct Assets {
    
    //可能要分每个界面对应的颜色, 先找出每个界面用到的所有的颜色, 让后看白色版本的对应的颜色
    
    struct Color {
        static let background = AppColor.background.value
        
        static let loginTypeTitleNormal = AppColor.loginTypeTitleNormal.value
        static let loginTypeTitleSelect = AppColor.loginTypeTitleSelect.value
        static let labeledTextFieldTitle = AppColor.labeledTextFieldTitle.value
        static let labeledTextFieldInput = AppColor.labeledTextFieldInput.value
        static let labeledTextFieldTip = AppColor.labeledTextFieldTip.value
        static let loginAgreementTitle = AppColor.loginAgreementTitle.value
        static let white = AppColor.appWhite.value
        static let black = AppColor.appBlack.value
        static let hex00CCFF = AppColor.hex00CCFF.value
        static let hex2E3036 = AppColor.hex2E3036.value
        static let hex2078D5 = AppColor.hex2078D5.value
        static let hex34C759 = AppColor.hex34C759.value
        
        
        static let guideTitle = AppColor.guideTitle.value
        static let newsTitle = AppColor.newsTitle.value

        static let selectCellBackgroundColor = AppColor.selectCellBackgroundColor.value
        
        static let selectedSegmentTintColor = AppColor.selectedSegmentTintColor.value
        static let segmentedControlTitle = AppColor.segmentedControlTitle.value
        static let segmentedControlSelectTitle = AppColor.segmentedControlSelectTitle.value
        static let segmentedControlBg = AppColor.segmentedControlBg.value
        
        static let tabbar_bg = AppColor.tabbar_bg.value
        static let tabbar_selectTitle = AppColor.tabbar_selectTitle.value
        static let tabbar_title = AppColor.tabbar_title.value
        
        static let tabbar_separator = AppColor.tabbar_separator.value


    }
    
    struct Image {
        static let styleMode = AppImage.styleMode.value
        static let loginLogo = AppImage.loginLogo.value
        static let nav_back = AppImage.nav_back.value
        static let languageIcon = AppImage.languageIcon.value
        static let tokenPriceRefresh = AppImage.tokenPriceRefresh.value
        
        static let nav_mine_info = AppImage.nav_mine_info.value
        static let nav_logo = AppImage.nav_logo.value
        static let nav_logout = AppImage.nav_logout.value
    }
}

final class ThemeManager {
    
    static let shared = ThemeManager()
    
    var currentStyle: UIUserInterfaceStyle {
        didSet {
            guard oldValue != currentStyle else { return }
            UserDefaults.standard.set(currentStyle.rawValue, forKey: "AppInterfaceStyle")
            UserDefaults.standard.synchronize()
            applyStyle()
            NotificationCenter.default.post(name: NotificationName.themeDidChange, object: currentStyle)
        }
    }
    
    private init() {
        
        if let saved = UserDefaults.standard.value(forKey: "AppInterfaceStyle") as? Int,
           let style = UIUserInterfaceStyle(rawValue: saved) {
            
            currentStyle = style
            
            if style == .light {
                printf("当前模式为浅色模式")
            } else if style == .dark {
                printf("当面模式为深色模式")
            } else {
                printf("当前模式为跟随系统模式")
            }

        } else {
            printf("当面模式为深色模式")
            currentStyle = .dark // 默认 dark
            UserDefaults.standard.set(currentStyle.rawValue, forKey: "AppInterfaceStyle")
            UserDefaults.standard.synchronize()
        }
    }
    
    func setStyle() {
        applyStyle()
    }
    
    func toggleStyle() {
        currentStyle = (currentStyle == .dark) ? .light : .dark
    }
    
    private func applyStyle() {
        UIApplication.shared.windows.forEach { window in
            window.overrideUserInterfaceStyle = currentStyle
            if currentStyle == .light {
                printf("切换为浅色模式")
            } else if currentStyle == .dark {
                printf("切换为深色模式")
            } else {
                printf("切换为跟随系统模式")
            }
        }
    }
}

